<template>
  <div class="login-container">
    <div class="login-form">
      <div class="login-subtitle">——eomni设备管理系统——</div>
      <h2>设备管理系统</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="0">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入账号"
            prefix-icon="User"
            size="large"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            size="large"
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            style="width: 100%"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

export default {
  name: 'Login',
  setup() {
    const router = useRouter()
    const loginFormRef = ref()
    const loading = ref(false)
    
    const loginForm = reactive({
      username: '',
      password: ''
    })
    
    const rules = {
      username: [
        { required: true, message: '请输入账号', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
      ]
    }
    
    const handleLogin = async () => {
      try {
        await loginFormRef.value.validate()
        loading.value = true
        
        const response = await axios.post('/api/auth/login', loginForm)
        
        if (response.data.token) {
          sessionStorage.setItem('token', response.data.token)
          sessionStorage.setItem('username', response.data.username)
          ElMessage.success('登录成功')
          router.push('/devices')
        }
      } catch (error) {
        if (error.response?.data?.message) {
          ElMessage.error(error.response.data.message)
        } else {
          ElMessage.error('登录失败，请重试')
        }
      } finally {
        loading.value = false
      }
    }
    
    return {
      loginForm,
      loginFormRef,
      rules,
      loading,
      handleLogin
    }
  }
}
</script>

<style scoped>
.login-subtitle {
  font-size: 14px;
  color: #999999;
  margin-bottom: 20px;
  text-align: center;
  position: relative;
}

.login-subtitle::before,
.login-subtitle::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 30px;
  height: 1px;
  background: #e4e7ed;
}

.login-subtitle::before {
  left: -40px;
}

.login-subtitle::after {
  right: -40px;
}

.el-input {
  margin-bottom: 20px;
}

.el-input__wrapper {
  height: 44px;
  border: 1px solid #e4e7ed;
  border-radius: 6px;
  background: #ffffff;
}

.el-input__wrapper:hover {
  border-color: #67c23a;
}

.el-input__wrapper.is-focus {
  border-color: #67c23a;
  box-shadow: 0 0 0 2px rgba(103, 194, 58, 0.2);
}

.el-button {
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 6px;
}
</style> 